﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Jerry.DroidBuilder.Common;
using System.Reflection;

namespace Jerry.DroidBuilder.Extensibility
{
    public static class PluginManager
    {
        public static IEnumerable<IPlugin> LoadPlugins()
        {
            var plugins = new List<IPlugin>();
            var pluginType = typeof(IPlugin);
            var assemblyFiles = Directory.GetFiles(Util.PluginDirectory, "*.dll");

            try
            {
                foreach (var fileName in assemblyFiles)
                {
                    var assembly = Assembly.LoadFile(fileName);

                    foreach (var type in assembly.GetTypes())
                    {
                        if (pluginType.IsAssignableFrom(type))
                        {
                            plugins.Add(Activator.CreateInstance(type) as IPlugin);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ExceptionLogger.Log(ex);
            }

            return plugins.ToArray();
        }
    }
}
